/*
  学习目标：setState的第二种写法
  语法: setState(preState => 新的state)
  特点: 会合并连续调用setState,
  合并: 等待上一次的计算结果, 类似串联的效果
  目的: 减少render执行次数, 避免频繁操作dom
 
*/
import React, { Component } from 'react';

export default class App extends Component {
  state = {
    count: 0,
  };

  handleAdd = () => {
    this.setState((preState) => {
      return {
        count: preState.count + 1,
      };
    });
    this.setState((preState) => {
      return {
        count: preState.count + 2,
      };
    });
    this.setState((preState) => {
      return {
        count: preState.count + 3,
      };
    });
    // 💥 还是异步的
    console.log('this.state.count  ----->  ', this.state.count);
  };
  render() {
    console.log('render被执行了  ----->  ');
    const { count } = this.state;
    return (
      <div>
        <h1>{count}</h1>
        <button onClick={this.handleAdd}>点我</button>
      </div>
    );
  }
}
